<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="编码：" name="code">
				<a-input v-model:value="formData.code" placeholder="请输入编码" allow-clear />
			</a-form-item>
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="预充值：" name="precharged">
				<a-input-number style="width: 160px" :disabled="!!formData.id || formData.cardProp === '2'" v-model:value="formData.precharged" :min="0" placeholder="请输入预充值金额" allow-clear />
			</a-form-item>
			<a-form-item label="卡属性：" name="cardProp">
				<a-select
					:disabled="!!formData.id"
					v-model:value="formData.cardProp"
					:options="categoryOptions"
					placeholder="请选择卡属性"></a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmCardTypeForm">
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			cardProp: '1',
			sortCode: 99
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.value.cardProp += ''
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		code: [required('请输入卡编码')],
		name: [required('请输入卡名称')],
		cardProp: [required('请选择卡类型')],
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()
	let categoryOptions = proxy.$TOOL
		.dictTypeList('CARD_PROP')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = Object.assign({}, formData.value)
				if (param.cardProp === '2') {
					param.precharged = 0
				}
				submitLoading.value = true
				crmApi.cardTypeSubmitForm(param, param.id).then(() => {
					visible = false
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
